package com.shensi.adminCollect.controller.customer;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.domain.entity.SysDept;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.customer.CustomerCardRecordConsumption;
import com.shensi.adminCollect.service.customer.ICustomerCardRecordConsumptionService;
import com.shensi.adminCollect.service.system.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

import java.util.List;

/**
 * 客户电子会员卡消费记录Controller
 *
 * @author qujia
 * @date 2021-04-13
 */
@RestController
@RequestMapping("/customer/consumption")
public class CustomerCardRecordConsumptionController extends BaseController
{
    @Autowired
    private ICustomerCardRecordConsumptionService customerCardRecordConsumptionService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询客户电子会员卡消费记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        if(customerCardRecordConsumption!=null &&customerCardRecordConsumption.getStationId()!=null){
            SysDept dept =new SysDept();
            dept.setDeptId(Long.valueOf(customerCardRecordConsumption.getStationId()));
            List<String> list = deptService.selectDeptId(dept);
            customerCardRecordConsumption.setStationIdList(list);
            customerCardRecordConsumption.setStationId(null);
        }
        startPage();
        List<CustomerCardRecordConsumption> list = customerCardRecordConsumptionService.selectCustomerCardRecordConsumptionList(customerCardRecordConsumption);
        return getDataTable(list);
    }

    /**
     * 导出客户电子会员卡消费记录列表
     */
    @GetMapping("/export")
    public AjaxResult export(CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        if(customerCardRecordConsumption!=null &&customerCardRecordConsumption.getStationId()!=null){
            SysDept dept =new SysDept();
            dept.setDeptId(Long.valueOf(customerCardRecordConsumption.getStationId()));
            List<String> list = deptService.selectDeptId(dept);
            customerCardRecordConsumption.setStationIdList(list);
            customerCardRecordConsumption.setStationId(null);
        }
        List<CustomerCardRecordConsumption> list = customerCardRecordConsumptionService.selectCustomerCardRecordConsumptionList(customerCardRecordConsumption);
        ExcelUtil<CustomerCardRecordConsumption> util = new ExcelUtil<CustomerCardRecordConsumption>(CustomerCardRecordConsumption.class);
        return util.exportExcel(list, "consumption");
    }

    /**
     * 获取客户电子会员卡消费记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerCardRecordConsumptionService.selectCustomerCardRecordConsumptionById(id));
    }

    /**
     * 新增客户电子会员卡消费记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardRecordConsumption.setStationId(currentUser.getUser().getDeptId());
        customerCardRecordConsumption.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerCardRecordConsumptionService.insertCustomerCardRecordConsumption(customerCardRecordConsumption));
    }

    /**
     * 修改客户电子会员卡消费记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardRecordConsumption.setStationId(currentUser.getUser().getDeptId());
        customerCardRecordConsumption.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerCardRecordConsumptionService.updateCustomerCardRecordConsumption(customerCardRecordConsumption));
    }

    /**
     * 删除客户电子会员卡消费记录
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerCardRecordConsumptionService.deleteCustomerCardRecordConsumptionByIds(ids));
    }

    /**
     * 获取客户电子会员卡消费汇总金额
     */
    @GetMapping(value = "xfAmtQuery")
    public AjaxResult xfAmtQuery(CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        if(customerCardRecordConsumption!=null &&customerCardRecordConsumption.getStationId()!=null){
            SysDept dept =new SysDept();
            dept.setDeptId(Long.valueOf(customerCardRecordConsumption.getStationId()));
            List<String> list = deptService.selectDeptId(dept);
            customerCardRecordConsumption.setStationIdList(list);
            customerCardRecordConsumption.setStationId(null);
        }
        return AjaxResult.success(customerCardRecordConsumptionService.xfAmtQuery(customerCardRecordConsumption));
    }

    /**
     * 获取客户电子会员卡消费汇总折线图x 轴数据
     */
    @GetMapping(value = "listXdata")
    public TableDataInfo listXdata(CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        if(customerCardRecordConsumption!=null &&customerCardRecordConsumption.getStationId()!=null){
            SysDept dept =new SysDept();
            dept.setDeptId(Long.valueOf(customerCardRecordConsumption.getStationId()));
            List<String> list = deptService.selectDeptId(dept);
            customerCardRecordConsumption.setStationIdList(list);
            customerCardRecordConsumption.setStationId(null);
        }
        List<CustomerCardRecordConsumption> list =customerCardRecordConsumptionService.listXdata(customerCardRecordConsumption);
        return getTable(list);
    }

    /**
     * 获取客户电子会员卡消费汇总折线图充值数据
     */
    @GetMapping(value = "/listQydataXF")
    public TableDataInfo listQydataXF(CustomerCardRecordConsumption customerCardRecordConsumption)
    {
        if(customerCardRecordConsumption!=null &&customerCardRecordConsumption.getStationId()!=null){
            SysDept dept =new SysDept();
            dept.setDeptId(Long.valueOf(customerCardRecordConsumption.getStationId()));
            List<String> list = deptService.selectDeptId(dept);
            customerCardRecordConsumption.setStationIdList(list);
            customerCardRecordConsumption.setStationId(null);
        }
        List<CustomerCardRecordConsumption> list =customerCardRecordConsumptionService.listQydataXF(customerCardRecordConsumption);
        return getTable(list);
    }
}
